www.gusucode.com > VC编写的串口调试软件 > VC编写的串口调试软件,内含Modbus协议类 支持对Modbus通讯调试/Modbus串口调试软件1.0/DlgVerity.cpp

    // DlgVerity.cpp : implementation file
//

#include "stdafx.h"
#include "modbusrtu.h"
#include "DlgVerity.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#include "Verity.h"

/////////////////////////////////////////////////////////////////////////////
// CDlgVerity dialog


CDlgVerity::CDlgVerity(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgVerity::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgVerity)
	m_strVerirtyChar = _T("");
	m_strVerityCode = _T("");
	m_nVerityType =CVerity::V_CRC16;
	//}}AFX_DATA_INIT
	m_nTxtType = COM_HEXBIN;

}


void CDlgVerity::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgVerity)
	DDX_Text(pDX, IDC_EDIT_VERITY_CHAR, m_strVerirtyChar);
	DDX_Text(pDX, IDC_EDIT_VERITY_CODE, m_strVerityCode);
	DDX_CBIndex(pDX, IDC_COMBO, m_nVerityType);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgVerity, CDialog)
	//{{AFX_MSG_MAP(CDlgVerity)
	ON_BN_CLICKED(IDC_BTN_CAlCU, OnBtnCalcu)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgVerity message handlers

void CDlgVerity::OnBtnCalcu() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
		
	CVerity Verity;
	int nTxtCount=0;

	if(COM_HEXBIN==m_nTxtType)
	{
		switch(	m_nVerityType)
		{
		//1  5a   0  11  e1  c7
		case CVerity::V_CRC16:
			{
			CString strTxt=m_strVerirtyChar;
	//		strTxt.r ("0x");
			CVerity Verity;

			nTxtCount=Verity.TransStrHex(strTxt,m_ucHexBuffer,500);
			unsigned short usCode=Verity.CRC16(m_ucHexBuffer,nTxtCount);
			ShowVerity(usCode,2);
			break;
			}
		case CVerity::V_LRC:
			{
				CString strTxt=m_strVerirtyChar;
				CVerity Verity;
				
				unsigned short usCode=Verity.LRC (strTxt);
				ShowVerity(usCode,1);
				break;
			}
		default:
			;
		}
	}


}


void CDlgVerity::ShowVerity(unsigned short usCode,unsigned int unCount)
{
	unsigned int unIndex;
//	unsigned int unCount=2;

	unsigned int ucHexBuffer[2];
	ucHexBuffer[0]=usCode & 0x00FF;	//低字节---在前
	ucHexBuffer[1]=usCode>>8;		//高字节---在后


	CString strTxt;
	CString strVeriryTxt;
	for(unIndex=0;unIndex<unCount;unIndex++)
	{
		unsigned char ucByteH=ucHexBuffer[unIndex]>>4;
		unsigned char ucByteL=ucHexBuffer[unIndex]&0x0F;
		
		if(ucByteH>=0xA)
			strTxt.Format ("0x%c",char(ucByteH-0x0A+'A'));
		else
			strTxt.Format ("0x%c",char(ucByteH+'0'));

		if(ucByteL>=0xA)
			strTxt+=char(ucByteL-0x0A+'A');
		else
			strTxt+=char(ucByteL+'0');
		strTxt+=" ";

		strVeriryTxt+=strTxt;

	}
	m_strVerityCode=strVeriryTxt;

	UpdateData(FALSE);
}

BOOL CDlgVerity::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	CComboBox * pWnd=(CComboBox * )GetDlgItem(IDC_COMBO);
	if(pWnd)
	{
		//
		CVerity Verity;
		for(int n=0;n<=Verity.m_asVerityType.GetUpperBound ();n++)
		{	
			pWnd->AddString (Verity.m_asVerityType.GetAt (n));
			
			
		}
	}
	pWnd->SetCurSel (0);
	UpdateData(TRUE);

	CenterWindow();
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}